package com.klcw.MuticastDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * @ClassName MuticastClient
 * @Description 组播客户端
 * @Author dukang
 * @Date 2019/1/21 14:55
 * @Version 1.0
 **/
public class MuticastClient {
    public static void main(String[] args) throws IOException {

        // 创建一个组加入
        InetAddress group = InetAddress.getByName("224.5.6.7");

        MulticastSocket socket = new MulticastSocket(8888);
        socket.joinGroup(group);

        byte[] bytes = new byte[256];
        while (true){
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
            socket.receive(datagramPacket);

            String s = new String(datagramPacket.getData());
            System.out.println("接收的数据是：" + s);

        }
    }
}
